<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.OutreqDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.OutpurmatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	OutreqDAO outreqDao = (OutreqDAO)ctx.getBean("OutreqDAO");
	OutpurmatDAO outpurmatDao = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	
	String pnStr = request.getParameter("pn");
	String searchType = request.getParameter("searchType");
	String searchKeyword = request.getParameter("searchKeyword");
	String searchDate = request.getParameter("searchDate");
	
	int uid = (Integer)session.getAttribute("id");
	Integer teamId = (Integer)session.getAttribute("tid");
	
	int pn = 0;
	int pagesize = 10;
	
	try{
		pn = Integer.parseInt(pnStr);
	}catch(Exception e){
		pn = 0;
	}
	
	List where = new ArrayList();
	
	if(searchKeyword != null && !"".equals(searchKeyword)){
		if("1".equals(searchType)){
			where.add(new Object[]{"like", "outCode", searchKeyword});
		}else if("2".equals(searchType)){
			where.add(new Object[]{"like", "p.code", searchKeyword});
		}
	}
	
	if(searchDate != null && !"".equals(searchDate)){
		where.add(new Object[]{"eq", "reqDate", new SimpleDateFormat("yyyy-MM-dd").parse(searchDate)});
	}
	
	List listob = new ArrayList();
	listob.add(new Object[]{"eq","phase",5});
	listob.add(new Object[]{"eq","phase",7});
	where.add(new Object[]{"or","",listob});
	
	where.add(new Object[]{"eq", "outReqState", 2});
	where.add(new Object[]{"eq", "teamId", teamId});
	where.add(new Object[]{"eq", "reqUserId", uid});
	
	Map map = outreqDao.getOutreq(pn, pagesize, 
			new String[][]{new String[]{"desc","outReqDate"}},
			where,
			new String[][]{new String[]{"","id"},new String[]{"","outCode"},new String[]{"","p.code"},new String[]{"","p.name"},new String[]{"","reqDate"},new String[]{"","outReqDate"}}		
			,new String[]{"project"});
	
	List outpurmats = new ArrayList();
	List outreqs = (List)map.get("data");
	for(int i=0;i<outreqs.size();i++){
		Map mapTmp = new HashMap();
		mapTmp.put("req",outreqs.get(i));
		Object[] object = (Object[])outreqs.get(i);
		List listTmp = outpurmatDao.findByOutReqIdAndStateDel(object[0],3);
		mapTmp.put("purmat",listTmp);
		outpurmats.add(mapTmp);
	}
	request.setAttribute("outpurmats",outpurmats);
	
	request.setAttribute("map",map);
	
	int pageTotal = ((Integer)map.get("pageTotal")).intValue();
	int pageNo = ((Integer)map.get("pageNo")).intValue();
	List pageList=getPage(pageTotal,pageNo);
	request.setAttribute("pages",pageList);
	
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchKeyword",searchKeyword);
	request.setAttribute("searchDate",searchDate);
	
	pageContext.forward("/vm/returnStock/retList.vm");
%>